import Vue from 'vue'
import Router from 'vue-router'
import home from '@/views/index'
import exam from '@/views/exam'
import result from '@/views/result'
import QRcode from '@/views/QRcode'

import {
  getWeChatUserInfo
} from '@/api/exam'
import WechatAuth from 'vue-wechat-auth'

////
localStorage.clear()

Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/',
      name: 'home',
      component: home,
      meta: {
        auth: true
      }
    },
    {
      path: '/exam',
      name: 'exam',
      component: exam,
      meta: {
        auth: true
      }
    },
    {
      path: '/result',
      name: 'result',
      component: result,
      meta: {
        auth: true
      }
    },
    {
      path: '/QRcode',
      name: 'QRcode',
      component: QRcode,
      meta: {
        auth: true
      }
    }
  ]
})

/**
 * 判断是否是微信
 */
const isWechat = () => {
  const checkResult = (/micromessenger/i).test(window.navigator.userAgent.toLowerCase())
  return checkResult;
};


if (isWechat()) {
  Vue.use(WechatAuth, {
    router,
    appid: 'wx3a8ba43ba9082fa3',
    responseType: 'code',
    scope: 'snsapi_userinfo',
    getCodeCallback(code, next) {
      const hostname = location.hostname;
      getWeChatUserInfo(code, hostname).then(res => {
        next(res.data.token);
      }).catch(() => {
        next();
      })
    }
  })
}

export default router
